﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class MissionHeaderRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<MissionHeader> GetAllData()
        {
            return from MissionHeader in db.MissionHeaders
                   orderby MissionHeader.MissionID descending
                   select MissionHeader;
        }


        public IQueryable<MissionHeader> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.MissionHeaders.Where(p => p.MissionID.ToString().Contains(searchString));
            if (searchField == "MissionID")
                ps = db.MissionHeaders.Where(p => p.MissionID.ToString().Contains(searchString));
            if (searchField == "TMR")
                ps = db.MissionHeaders.Where(p => p.TMR.Contains(searchString));
            return ps.AsQueryable();

        }
        public IQueryable<MissionHeader> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }



        public MissionHeader GetByID(long id)
        {
            return db.MissionHeaders.SingleOrDefault(MissionHeader => MissionHeader.MissionID == id);
        }

        //Inset
        public void Add(MissionHeader MissionHeader)
        {
            //if (MissionHeader.StatusID == 0 )
            //    MissionHeader.StatusID = 1;
            MissionHeader.InsertedUser = HttpContext.Current.User.Identity.Name;
            MissionHeader.InsertedDate = DateTime.Now;
            MissionHeader.UpdatedUser = HttpContext.Current.User.Identity.Name;
            MissionHeader.UpdatedDate = DateTime.Now;
            MissionHeader.DataVersion = 1;

            db.MissionHeaders.AddObject(MissionHeader);
        }

        //Update
        public void Update(MissionHeader MissionHeader)
        {

            MissionHeader originalMissionHeader = GetByID(MissionHeader.MissionID);

            MissionHeader.InsertedUser = originalMissionHeader.InsertedUser;
            MissionHeader.InsertedDate = originalMissionHeader.InsertedDate;
            MissionHeader.UpdatedUser = HttpContext.Current.User.Identity.Name;
            MissionHeader.UpdatedDate = DateTime.Now;
            MissionHeader.DataVersion = ++originalMissionHeader.DataVersion;
  
            db.MissionHeaders.ApplyCurrentValues(MissionHeader);
        }

        //Delete
        public void Delete(MissionHeader MissionHeader)
        {
            db.MissionHeaders.DeleteObject(MissionHeader);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}